/**
 * 
 */
package leetCode;

import java.util.HashMap;
import java.util.Map;

/**
 * @author zhong
 *
 */
class UncommonWordsfromTwoSentences {
	int size = 0;

	public String[] uncommonFromSentences(String A, String B) {
		Map<String, Boolean> onceMap = new HashMap<>();
		helper(A, onceMap);
		helper(B, onceMap);
		String[] reStrings = new String[size];
		for (String string : onceMap.keySet()) {
			if (onceMap.get(string)) {
				reStrings[--size] = string;
			}
		}
		return reStrings;
	}

	private void helper(String a, Map<String, Boolean> onceMap) {
		String[] aStrings = a.split(" ");
		for (String string : aStrings) {
			if (onceMap.containsKey(string)) {// 如果是第二次：false；如果是第三次：
				if (onceMap.get(string) == true) {
					size--;
				}
				onceMap.put(string, false);
			} else {
				onceMap.put(string, true);// 第一次出现
				size++;
			}
		}
	}

	public static void main(String[] args) {
		String A = "s z z z s", B = "s z ejt";
		String[] strings = new UncommonWordsfromTwoSentences().uncommonFromSentences(A, B);
		for (String string : strings) {
			System.out.println(string);
		}
	}
}
